#include <main.h>


void inc_button_short_press() {
    led_pos = max(0, led_pos - 1);
    digitalWrite(led_pins[led_pos], LOW);
}

void inc_button_long_press() {
    inc_button_is_long_pressed = true;
}

void inc_button_long_press_precess() {
    if (inc_button.get_key_state() != Button::KeyState::LONG_PRESS) {
        inc_button_is_long_pressed = false;
    }

    const uint32_t cur_millis = millis();
    static uint32_t pre_millis = 0;
    static const uint32_t interval = 100;
    if (cur_millis - pre_millis < interval) return ;
    pre_millis = cur_millis;

    inc_button_short_press();
}

void dec_button_short_press() {
    digitalWrite(led_pins[led_pos], HIGH);
    led_pos = min(8, led_pos + 1);
}

void dec_button_long_press() {
    dec_button_is_long_pressed = true;
}

void dec_button_long_press_precess() {
    if (dec_button.get_key_state() != Button::KeyState::LONG_PRESS) {
        dec_button_is_long_pressed = false;
    }

    const uint32_t cur_millis = millis();
    static uint32_t pre_millis = 0;
    static const uint32_t interval = 100;
    if (cur_millis - pre_millis < interval) return ;
    pre_millis = cur_millis;

    dec_button_short_press();
}


void setup_led() {
    for (auto pin : led_pins) {
        pinMode(pin, OUTPUT);
        digitalWrite(pin, HIGH);
    }
}

void setup_button() {
    inc_button.begin(BUTTON_DETECTION_PERIOD);
    dec_button.begin(BUTTON_DETECTION_PERIOD);
}

void loop_button() {
    inc_button.run();
    dec_button.run();
    if (inc_button_is_long_pressed) inc_button_long_press_precess();
    if (dec_button_is_long_pressed) dec_button_long_press_precess();
}

void setup() {
    setup_led();
    setup_button();
}

void loop() {
    loop_button();
}
